package sunstar.com.xq.activity

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.v7.graphics.Palette
import android.support.v7.widget.LinearLayoutManager
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
import kotlinx.android.synthetic.main.activity_video_info_adapter.*

import sunstar.com.xq.R
import sunstar.com.xq.adapter.CommentAdapter
import sunstar.com.xq.customview.FullyLinearLayoutManager
import sunstar.com.xq.databean.CardData
import sunstar.com.xq.databean.CommentListData
import sunstar.com.xq.presenter.CommentPersenter
import sunstar.com.xq.util.AppBarStateChangeListener
import sunstar.com.xq.view.CommentView
import kotlin.Exception

class VideoInfoActivity : BaseActivity(),CommentView {

    private var adapter:CommentAdapter?=null
    private var list = mutableListOf<CommentListData>()
    private var presenter:CommentPersenter?=null

    override fun initData() {
        video_swip_layout.isRefreshing = false
        shimmer_layout.hideShimmerAdapter()
        adapter!!.notifyDataSetChanged()
    }

    override fun setBaseView(): Int = R.layout.activity_video_info_adapter

    override fun setTitleText(): String =""
    override fun showTitle(): Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        JCVideoPlayer.ACTION_BAR_EXIST = false
        JCVideoPlayer.TOOL_BAR_EXIST = false
        setView()
        presenter!!.getData()

    }
    private fun setView() {
        var data = intent.getSerializableExtra("data") as CardData
        presenter = CommentPersenter(this,data.objectId,list)
        video_toolbar_layout.title = data.cardTtitle
        video_toolbar_layout.setContentScrimColor(resources.getColor(R.color.colorPrimary))
        video_player.setUp(data.cardVideo, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL,"")
        video_player.seekToInAdvance = 1
        //关闭recycleview滑动
        val manager = object :LinearLayoutManager(this)
        {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        manager.setOrientation(LinearLayoutManager.VERTICAL)
        manager.setSmoothScrollbarEnabled(false)
        manager.setAutoMeasureEnabled(true)
        //设置管理器
        shimmer_layout.layoutManager = manager
        //实例化适配器
        adapter = CommentAdapter(this, list)
        //设置适配器
        shimmer_layout.adapter = adapter
        //显示等待布局
        shimmer_layout.showShimmerAdapter()
        //加载缩略图并提提取颜色设置背景
        Picasso.with(this@VideoInfoActivity).load(data.cardImg)!!.into(object : Target {

            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(errorDrawable: Drawable?) {
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                video_player.thumbImageView.setImageBitmap(bitmap)
                var bulid = Palette.from(bitmap)
                var palette = bulid.generate()
                var s1 = palette.mutedSwatch
                var s2 = palette.lightVibrantSwatch
                var s3 = palette.vibrantSwatch
                try {
                    activity_video_info.setBackgroundColor(s1!!.rgb)
                    video_appbar_layout.setBackgroundColor(s2!!.rgb)
                    video_toolbar_layout.setContentScrimColor(s2!!.rgb)
                    video_toolbar_layout.setExpandedTitleColor(s2.titleTextColor)
                    video_toolbar_layout.setCollapsedTitleTextColor(s3!!.titleTextColor)
//                    video_text.setTextColor(s3!!.bodyTextColor)
                } catch (e: Exception) {

                }
            }
        })
        //监听折叠布局实现播放器交互
        video_appbar_layout.addOnOffsetChangedListener(object : AppBarStateChangeListener (){

            override fun onStateChanged(appBarLayout: AppBarLayout?, state: State?) {
                if(state == State.COLLAPSED){
                    video_player.startWindowTiny()
                }else if(state == State.EXPANDED){
                    try {
                        video_player.playOnThisJcvd()
                    }catch (e:Exception){}
                }
            }

        })
        video_swip_layout.setOnRefreshListener {
            presenter!!.refresh()
        }
    }

    override fun onPause() {
        super.onPause()
        JCVideoPlayer.releaseAllVideos();
    }

    override fun onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return
        }
        super.onBackPressed()

    }

}
